package fr.charlott.batch.mapper;

import org.apache.commons.lang.StringUtils;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;

import fr.charlott.core.repository.model.Reunion;
import fr.charlott.core.service.HotesseService;
import fr.charlott.tools.Constants;

public class ReunionFieldSetMapper implements FieldSetMapper<Reunion> {

    private static final String FIELD_DATE = "date";
    private static final String FIELD_HOTESSE = "hotesse";
    private static final String FIELD_CA = "chiffre_affaire";

    @Autowired
    HotesseService hotesseService;

    @Override
    public Reunion mapFieldSet(FieldSet fieldSet) throws BindException {
        Reunion reunion = new Reunion();
        reunion.setDate(fieldSet.readDate(FIELD_DATE, Constants.DATE_PATTERN));
        if (StringUtils.isNotEmpty(fieldSet.readString(FIELD_HOTESSE))) {
            String[] arrayHotesse = StringUtils.split(fieldSet.readString(FIELD_HOTESSE).trim(), ":", 2);
            reunion.setHotesse(hotesseService.chercherParNomPrenom(arrayHotesse[1], arrayHotesse[0]));
        }
        reunion.setChiffreAffaire(fieldSet.readDouble(FIELD_CA));
        return reunion;
    }

}
